#include <string>
#include <iostream> // cout
#include <cstdlib>  // exit()

#include <fltk/ask.h>
#include <fltk/Monitor.h>
#include <fltk/xpmImage.h>

#include <profo/version.h>
#include <profo/ModelChooserUI.h>
#include <profo/AminoAcid.h> // TODO: this should be removed - for testing purposes only
#include <profo/SimpleHP2DRectangularAlgorithm.h> // controller

#include "../gfx/model_chooser_background.xpm"

using namespace profo;

void exitButtonReleaseCb(fltk::Button* w, void* v) {
  exit(0);
}

/**
 * In this callback function we create the main controller object and associate
 * it with automatically-generated Protein model (proteinModel) object.
 * Similarly, after generation of the controller, we create the main view object
 * as well, and pass the model to it.
 */
void hp2DRecReleaseCb(fltk::Button* w, void* v) {
  ModelChooserUI* o = reinterpret_cast<ModelChooserUI*>(v);
  SimpleHP2DRectangularAlgorithm* alg = new SimpleHP2DRectangularAlgorithm(o->proteinModel);
  o->controller = alg;
#ifdef DEBUG
  std::cout << o->proteinModel.toString() << std::endl;
  std::cout << o->proteinModel.get(1).toString() << std::endl;
#endif
  HP2DRectangularView *win = new HP2DRectangularView(o->proteinModel, *o->controller);
  win->show();
} // hp2DRecReleaseCb() callback

void hp2DTriangularReleaseCb(fltk::Button* w, void* v) {
  fltk::alert("Not yet implemented.");
}

void hpUdaReleaseCb(fltk::Button* w, void* v) {
  fltk::alert("Not yet implemented.");
}

void testButtonReleaseCb(fltk::Button* w, void* v) {
#ifdef DEBUG
  Protein::unittest();
#endif
}

ModelChooserUI::ModelChooserUI() {
  fltk::Window* w;
  std::string title(AutoVersion::FULLVERSION_STRING);
  title = "Profo v" + title;

  {fltk::Window* o = _window = new fltk::Window(400, 500, "Profo 0.1");
    w = o;
    // we must use copy_label() here because title will be deleted
    // when it goes out of scope
    o->copy_label(title.c_str());
    // put our window in the center of the screen.
    o->Rectangle::set(fltk::Monitor::find(0,0),o->w(),o->h(),fltk::ALIGN_CENTER);
    o->set_vertical();
    o->shortcut(0xff1b);
    o->user_data((void*)(this));
    o->begin();
      {fltk::Group* o = new fltk::Group(0, 0, 400, 500);
      o->set_vertical();
      o->image(new fltk::xpmImage(model_chooser_background_xpm));
      o->buttonbox(fltk::NO_BOX);
      o->begin();
        {fltk::ReturnButton* o = exitButon = new fltk::ReturnButton(310, 375, 70, 35, "E&xit");
        o->shortcut(0x80078);
        o->callback((fltk::Callback*)exitButtonReleaseCb);
        o->tooltip("Press this button to exit the application.");
        }
        {fltk::ReturnButton* o = testButon = new fltk::ReturnButton(210, 375, 70, 35, "&Test");
        o->shortcut(0x80078);
        o->callback((fltk::Callback*)testButtonReleaseCb);
        o->tooltip("For testing purposes only");
#ifdef DEBUG
        o->hide();
#endif
        }
        {fltk::Button* o = new fltk::Button(90, 190, 290, 45, "H-P Model, 2D Lattice, Linear search algorithm");
        o->box(fltk::THIN_UP_BOX);
        o->buttonbox(fltk::HIGHLIGHT_UP_BOX);
        o->callback((fltk::Callback*)hp2DRecReleaseCb, (void*)(this));
        o->align(fltk::ALIGN_LEFT|fltk::ALIGN_INSIDE);
        }
        {fltk::Button* o = new fltk::Button(90, 245, 290, 40, "H-P Model, 2D Triangular Lattice, LSA");
        o->buttonbox(fltk::THIN_UP_BOX);
        o->callback((fltk::Callback*)hp2DTriangularReleaseCb);
        o->align(fltk::ALIGN_LEFT|fltk::ALIGN_INSIDE);
        o->deactivate();
        }
        {fltk::Button* o = new fltk::Button(90, 310, 290, 40, "H-P Model, User-Defined Lattice&&Algorithm");
        o->buttonbox(fltk::THIN_UP_BOX);
        o->callback((fltk::Callback*)hpUdaReleaseCb);
        o->align(fltk::ALIGN_LEFT|fltk::ALIGN_INSIDE);
        o->deactivate();
        }
      o->end();
      }
    o->end();
    o->resizable(o);
  }
}

void ModelChooserUI::show(int argc, char** argv) {
  _window->show(argc, argv);
}

// $Id: ModelChooserUI.cpp 43 2009-04-26 11:52:53Z dejan.lekic $
